#!/bin/bash
# CUDA GEMM 调试快捷脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}CUDA GEMM 调试助手${NC}"
echo -e "${BLUE}================================${NC}"
echo ""

# 显示菜单
echo "请选择调试方式："
echo ""
echo "1) 编译 Debug 版本"
echo "2) 编译 Release 版本"
echo "3) 使用 CUDA-GDB 调试（小矩阵 512x512x512）"
echo "4) 使用 CUDA-GDB 调试（中等矩阵 1024x1024x1024）"
echo "5) Printf 调试（运行 debug 版本）"
echo "6) 性能分析（Nsight Compute）"
echo "7) 内存检查（Compute Sanitizer - memcheck）"
echo "8) 竞态条件检查（Compute Sanitizer - racecheck）"
echo "9) 查看调试指南"
echo "0) 退出"
echo ""

read -p "请输入选项 [0-9]: " choice

case $choice in
    1)
        echo -e "${GREEN}[INFO] 编译 Debug 版本...${NC}"
        cd "$SCRIPT_DIR"
        xmake f -m debug --root
        xmake --root
        echo -e "${GREEN}[完成] Debug 版本编译完成！${NC}"
        echo "路径: build/linux/x86_64/debug/gemm_tutorial"
        ;;
    
    2)
        echo -e "${GREEN}[INFO] 编译 Release 版本...${NC}"
        cd "$SCRIPT_DIR"
        xmake f -m release --root
        xmake --root
        echo -e "${GREEN}[完成] Release 版本编译完成！${NC}"
        echo "路径: build/linux/x86_64/release/gemm_tutorial"
        ;;
    
    3)
        echo -e "${YELLOW}[INFO] 启动 CUDA-GDB (512x512x512)...${NC}"
        echo "常用命令："
        echo "  - run                : 运行程序"
        echo "  - break main         : 在 main 设置断点"
        echo "  - continue           : 继续执行"
        echo "  - info cuda threads  : 查看 CUDA 线程"
        echo "  - quit               : 退出"
        echo ""
        cd "$SCRIPT_DIR"
        cuda-gdb --args ./build/linux/x86_64/debug/gemm_tutorial 512 512 512
        ;;
    
    4)
        echo -e "${YELLOW}[INFO] 启动 CUDA-GDB (1024x1024x1024)...${NC}"
        cd "$SCRIPT_DIR"
        cuda-gdb --args ./build/linux/x86_64/debug/gemm_tutorial 1024 1024 1024
        ;;
    
    5)
        echo -e "${GREEN}[INFO] 运行 Debug 版本（用于查看 printf 输出）...${NC}"
        read -p "输入矩阵大小 M N K (默认: 512 512 512): " M N K
        M=${M:-512}
        N=${N:-512}
        K=${K:-512}
        cd "$SCRIPT_DIR"
        ./build/linux/x86_64/debug/gemm_tutorial $M $N $K
        ;;
    
    6)
        echo -e "${BLUE}[INFO] 性能分析（Nsight Compute）...${NC}"
        read -p "输入矩阵大小 M N K (默认: 2048 2048 2048): " M N K
        M=${M:-2048}
        N=${N:-2048}
        K=${K:-2048}
        echo ""
        echo "选择分析模式："
        echo "  1) 快速分析（所有 kernels）"
        echo "  2) 详细分析 V2 Naive WMMA"
        echo "  3) 详细分析 V3 Pipeline WMMA"
        read -p "选择 [1-3]: " ncu_choice
        
        cd "$SCRIPT_DIR"
        case $ncu_choice in
            1)
                ncu ./build/linux/x86_64/release/gemm_tutorial $M $N $K
                ;;
            2)
                ncu --set full --kernel-name v2_naive_wmma_matmul \
                    ./build/linux/x86_64/release/gemm_tutorial $M $N $K
                ;;
            3)
                ncu --set full --kernel-regex "v3_pipeline" \
                    ./build/linux/x86_64/release/gemm_tutorial $M $N $K
                ;;
        esac
        ;;
    
    7)
        echo -e "${YELLOW}[INFO] 内存检查（Compute Sanitizer）...${NC}"
        read -p "输入矩阵大小 M N K (默认: 1024 1024 1024): " M N K
        M=${M:-1024}
        N=${N:-1024}
        K=${K:-1024}
        cd "$SCRIPT_DIR"
        compute-sanitizer --tool memcheck \
            ./build/linux/x86_64/debug/gemm_tutorial $M $N $K
        ;;
    
    8)
        echo -e "${YELLOW}[INFO] 竞态条件检查（Compute Sanitizer）...${NC}"
        read -p "输入矩阵大小 M N K (默认: 1024 1024 1024): " M N K
        M=${M:-1024}
        N=${N:-1024}
        K=${K:-1024}
        cd "$SCRIPT_DIR"
        compute-sanitizer --tool racecheck \
            ./build/linux/x86_64/debug/gemm_tutorial $M $N $K
        ;;
    
    9)
        echo -e "${BLUE}[INFO] 打开调试指南...${NC}"
        cd "$SCRIPT_DIR"
        cat DEBUG_GUIDE.md | less
        ;;
    
    0)
        echo -e "${GREEN}退出${NC}"
        exit 0
        ;;
    
    *)
        echo -e "${RED}[错误] 无效选项${NC}"
        exit 1
        ;;
esac

echo ""
echo -e "${GREEN}[完成]${NC}"
